package com.hippo.core.service;

import com.alibaba.fastjson.JSONObject;
import com.hippo.core.dao.HomeDao;
import com.hippo.core.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author: hippo
 */
@Service
public class HomeService {

    @Autowired
    private HomeDao homeDao;

    public JSONObject getHomeData(String id) {
        List<JSONObject> latestCaseCnt = homeDao.getLatestCaseCnt(id);
        List<JSONObject> latestPlanCnt = homeDao.getLatestPlanCnt(id);
        List<JSONObject> casePlanRunCnt = homeDao.getCasePlanRunCnt(id);
        List<JSONObject> caseFailCnt = homeDao.getCaseFailCnt(id);
        List<JSONObject> planFailCnt = homeDao.getPlanFailCnt(id);
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> dateList = new ArrayList<>();
        ArrayList<Integer> caseIncrementList = new ArrayList<>();
        ArrayList<Integer> planIncrementList = new ArrayList<>();
        ArrayList<Integer> caseRunCntList = new ArrayList<>();
        ArrayList<Integer> planRunCntList = new ArrayList<>();
        ArrayList<Integer> caseFailCntList = new ArrayList<>();
        ArrayList<Integer> planFailCntList = new ArrayList<>();
        ArrayList<String> caseNameFailList = new ArrayList<>();
        ArrayList<String> planNameFailList = new ArrayList<>();
        latestCaseCnt.forEach(item -> {
            dateList.add(item.getString("dat"));
            caseIncrementList.add(item.getInteger("cnt"));
        });
        latestPlanCnt.forEach(item -> {
            planIncrementList.add(item.getInteger("cnt"));
        });
        casePlanRunCnt.forEach(item -> {
            caseRunCntList.add(item.getInteger("caseCnt"));
            planRunCntList.add(item.getInteger("planCnt"));
        });
        caseFailCnt.forEach(item -> {
            caseNameFailList.add(item.getString("name"));
            caseFailCntList.add(item.getInteger("failCnt"));
        });
        planFailCnt.forEach(item -> {
            planNameFailList.add(item.getString("name"));
            planFailCntList.add(item.getInteger("failCnt"));
        });
        map.put("dateList", dateList);
        map.put("caseIncrementList", caseIncrementList);
        map.put("planIncrementList", planIncrementList);
        map.put("caseCntList", homeDao.getCaseCnt(id));
        List<JSONObject> planCnt = homeDao.getPlanCnt(id);
        map.put("planCntList", planCnt);
        map.put("caseRunCntList", caseRunCntList);
        map.put("planRunCntList", planRunCntList);
        map.put("caseNameFailList", caseNameFailList);
        map.put("caseFailCntList", caseFailCntList);
        map.put("planNameFailList", planNameFailList);
        map.put("planFailCntList", planFailCntList);
        return CommonUtil.successJson(map);
    }
}
